<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="${class}">
    <typeAlias alias="${class?cap_first}" type="${package}.${class?cap_first}"/>

    <sql id="qrySelect">
        ${fieldAndSrcNames}
    </sql>

    <select id="selectById" resultClass="${class}">
        <include refid="qrySelect"/>
        from ${tableName}
        where
        sid=#sid#
    </select>

    <insert id="insert">
        <selectKey resultClass="Integer" keyProperty="sid">
            select ${tableName?upper_case}_SEQ.nextval AS keykey from dual
        </selectKey>
        insert into ${tableName} (
        ${fieldNames}
        ) values (
        ${srcNames}
        )
    </insert>

    <update id="updateById">
        update ${tableName}
        <dynamic prepend="set">
        <#list properties as prop>
            <isNotNull prepend="," property="${prop.name?uncap_first}">
                ${prop.srcName}=#${prop.name?uncap_first}#
            </isNotNull>
        </#list>
        </dynamic>
        where ${key}
    </update>

    <delete id="deleteById">
        delete from ${tableName} where ${key}
    </delete>


    <sql id="dynamicWhere">
        <dynamic prepend="where">
        <#list properties as prop>
            <isPropertyAvailable property="${prop.name?uncap_first}">
                <isNotEmpty prepend="and" property="${prop.name?uncap_first}">
                    ${prop.srcName}=#${prop.name?uncap_first}#
                </isNotEmpty>
            </isPropertyAvailable>
        </#list>
        </dynamic>
    </sql>

    <sql id="dynamicOrderBy">
        <isPropertyAvailable property="orderBy">
            <isNotEmpty prepend=" order by " property="orderBy">
                $orderBy$
            </isNotEmpty>
        </isPropertyAvailable>
    </sql>

    <select id="selectByCond" resultClass="${class}">
        <include refid="qrySelect"/>
        from ${tableName}
        <include refid="dynamicWhere"/>
        <include refid="dynamicOrderBy"/>
    </select>

    <select id="selectCountByCond" resultClass="Integer">
        select count(1) as kk
        from ${tableName}
        <include refid="dynamicWhere"/>
    </select>

</sqlMap>
